package com.huayun.plugin.fastener.utils;

import java.util.ArrayList;
import java.util.List;

public class QuarticEquationSolver {
    // 定义精度
    private static final double EPSILON = 1e-10;

    public static void main(String[] args) {
        // 示例方程: x^4 - 5x^3 + 5x^2 + 5x - 6 = 0
        double a = 1, b = -5, c = 5, d = 5, e = -6;

        List<Double> roots = solveQuartic(a, b, c, d, e);

        System.out.println("方程的根为：");
        for (double root : roots) {
            System.out.println(root);
        }
    }

    public static List<Double> solveQuartic(double a, double b, double c, double d, double e) {
        List<Double> roots = new ArrayList<>();
        if (Math.abs(a) < EPSILON) {
            // 如果a接近于0，则退化为三次方程
            return solveCubic(b, c, d, e);
        }

        // 使用多项式除法和 Newton-Raphson 法寻找所有实根
        double[] coefficients = {a, b, c, d, e};
        while (coefficients.length > 1) {
            double root = findRealRoot(coefficients);
            if (root != Double.NaN) {
                roots.add(root);
                // 使用多项式除法降阶
                coefficients = syntheticDivision(coefficients, root);
            } else {
                break; // 如果找不到更多实根，停止
            }
        }

        return roots;
    }

    private static double findRealRoot(double[] coefficients) {
        // 使用 Newton-Raphson 方法寻找一个实根
        double initialGuess = 1.0; // 初始猜测值
        int maxIterations = 1000;
        for (int i = 0; i < maxIterations; i++) {
            double fx = evaluatePolynomial(coefficients, initialGuess);
            double dfx = evaluateDerivative(coefficients, initialGuess);
            if (Math.abs(dfx) < EPSILON) {
                break; // 避免除以零
            }
            double nextX = initialGuess - fx / dfx;
            if (Math.abs(nextX - initialGuess) < EPSILON) {
                return nextX;
            }
            initialGuess = nextX;
        }
        return Double.NaN; // 如果找不到实根，返回 NaN
    }

    private static double[] syntheticDivision(double[] coefficients, double root) {
        // 多项式除法（综合除法）
        double[] result = new double[coefficients.length - 1];
        result[0] = coefficients[0];
        for (int i = 1; i < result.length; i++) {
            result[i] = coefficients[i] + result[i - 1] * root;
        }
        return result;
    }

    private static double evaluatePolynomial(double[] coefficients, double x) {
        // 计算多项式的值
        double result = 0;
        for (int i = 0; i < coefficients.length; i++) {
            result += coefficients[i] * Math.pow(x, coefficients.length - 1 - i);
        }
        return result;
    }

    private static double evaluateDerivative(double[] coefficients, double x) {
        // 计算多项式导数的值
        double result = 0;
        for (int i = 0; i < coefficients.length - 1; i++) {
            result += (coefficients.length - 1 - i) * coefficients[i] * Math.pow(x, coefficients.length - 2 - i);
        }
        return result;
    }

    private static List<Double> solveCubic(double a, double b, double c, double d) {
        // 简单实现三次方程求解（可以扩展）
        List<Double> roots = new ArrayList<>();
        // 使用Cardano公式或其他方法求解三次方程
        return roots;
    }
}
